//
//  ContentView.swift
//  ParseJSON
//
//  Created by alanzhangg on 2023/5/11.
//

import SwiftUI
import SDWebImageSwiftUI
import Alamofire
import SwiftyJSON

struct ContentView: View {
    
    @ObservedObject var obs = Observer()
    
    var body: some View {
        
        NavigationView {
            List(obs.datas) { i in
                Card(name: i.name, url: i.url)
            }
            .navigationTitle("JSON Parse")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

class Observer: ObservableObject{
    
    @Published var datas = [Datatype]()
    
    init(){
        AF.request("https://api.github.com/users/hadley/orgs").response { data in
            do{
                let json = try JSON(data: data.data!)
                for i in json{
                    self.datas.append(Datatype(id: i.1["id"].intValue, name: i.1["login"].stringValue, url: i.1["avatar_url"].stringValue))
                }
            }catch{
                
            }
            
        }
       
        
    }
    
}

struct Datatype: Identifiable{
    var id: Int
    var name: String
    var url: String
}

struct Card: View {
    
    var name = ""
    var url = ""
    
    var body: some View{
        
        HStack{
            AnimatedImage(url: URL(string: url)!)
                .resizable()
                .frame(width: 60, height: 60)
                .clipShape(Circle())
                .shadow(radius: 20)
            
            Text(name).fontWeight(.heavy)
            
        }
        
    }
}
